package com.airbnb.mvrx.sample;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Window;

import com.airbnb.mvrx.sample.util.QMUIStatusBarHelper;
import com.example.mvrxstudy.R;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import timber.log.Timber;

public class Utils {

    public static String string(Object obj) {
        return format("%s", obj.toString());
    }

    public static String format(String format, Object... args) {
        return String.format(Locale.CHINA, format, args);
    }

    public static DisplayMetrics getDM(Context context) {
        return context.getResources().getDisplayMetrics();
    }

    public static int getContentHeight(Context context) {
        return getDM(context).heightPixels - QMUIStatusBarHelper.getStatusbarHeight(context);
    }

    public static int getToolBarHeight(Context context) {
        int[] attrs = new int[]{R.attr.actionBarSize};
        TypedArray ta = context.obtainStyledAttributes(attrs);
        int toolBarHeight = ta.getDimensionPixelSize(0, 50);
        ta.recycle();
        return toolBarHeight;
    }

    public static int dp2px(Context context, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp * 1f,
                getDM(context));
    }

    public static Map<String, Integer> getStatusBarHeight(Activity activity) {
        Rect rectangle = new Rect();
        Window window = activity.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
        int statusBarHeight = rectangle.top;
        int contentViewTop =
                window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
        int titleBarHeight = contentViewTop - statusBarHeight;
        Map<String, Integer> result = new HashMap<>();
        result.put("statusBarHeight", statusBarHeight);
        result.put("titleBarHeight", titleBarHeight);
        return result;
    }

    public static Map<String, Object> getCommonParam() {
        Map<String, Object> map = new HashMap<>();
        map.put("_appn", "miniApp");
        map.put("_appv", "81");
        map.put("_tid", "");
        map.put("_appt", "2");
        map.put("_channel", "wxMinApp");
        map.put("_appid", 0);
        map.put("_templateid", 123);
        map.put("_sign", "566063");
        return map;
    }

    public static void log(String format, Object... args) {
        Timber.d(format(format, args));
    }

    public static String imageUrlFromResId(String host, Long resId, String resIdSign) {
        return Utils.format("%s/resourceservice/mediaplay.do?resId=%d&resIdSign=%s&mediaType=4",
                host, resId, resIdSign);
    }


    public static String BASE_URL = "https://wxmini-prepub.bookln.cn";

}
